home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 302.adf / ProgUtils / Signal.asm < prev    next >
Assembly Source File  |  1990-01-15  |  6KB  |  318 lines

  1.             opt    c+,l-
  2.  
  3.     ;***
  4.     ;Signal a task
  5.     ;© J.Tyberghein (C version                30 sep 89)
  6.     ;© J.Tyberghein (Conversion to ML     2 oct 89)
  7.     ;***
  8.  
  9.             incdir    include/
  10.             include    libraries/dosextens.i
  11.             include    exec/execbase.i
  12.  
  13. SysBase            equ    4
  14.     ;ExecBase routines
  15. _LVOOldOpenLibrary    equ    -408
  16. _LVOCloseLibrary        equ    -414
  17. _LVOForbid                equ    -132
  18. _LVOPermit                equ    -138
  19. _LVOSignal                equ    -324
  20.     ;DosBase routines
  21. _LVOWrite                equ    -48
  22. _LVOOutput                equ    -60
  23.  
  24.  
  25. CALLEXEC    macro
  26.             move.l (SysBase).w,a6
  27.             jsr _LVO\1(a6)
  28.             endm
  29.  
  30. CALLDOS    macro
  31.             move.l DosBase,a6
  32.             jsr _LVO\1(a6)
  33.             endm
  34.  
  35.     ;Main program
  36.         move.l    a0,ArgPointer
  37.         move.l    d0,ArgLen
  38.     ;Open libraries
  39.         lea        DosLib,a1
  40.         CALLEXEC    OldOpenLibrary
  41.         move.l    d0,DosBase
  42.     ;Get outputhandle
  43.         CALLDOS    Output
  44.         move.l    d0,OutputHandle
  45.         bsr        Main
  46.     ;Close libraries
  47.         move.l    DosBase,a1
  48.         CALLEXEC    CloseLibrary
  49.         rts
  50.  
  51.     ;*** String length ***
  52.     ;a0 = string address
  53.     ;-> d0 = length
  54.     ;***
  55. StrLen:
  56.         moveq        #-1,d0
  57. LoopSL:
  58.         addq.l    #1,d0
  59.         tst.b        (a0)+
  60.         bne.s        LoopSL
  61.         rts
  62.  
  63.     ;*** Put a message on the screen ***
  64.     ;a0 = message
  65.     ;***
  66. Message:
  67.         movem.l    a0,-(a7)
  68.         bsr        StrLen
  69.         movem.l    (a7)+,a0
  70.         move.l    d0,d3
  71.         move.l    OutputHandle,d1
  72.         move.l    a0,d2
  73.         CALLDOS    Write
  74.         rts
  75.  
  76.     ;*** Compare a BSTR with an CSTR ***
  77.     ;a2 = BSTR
  78.     ;a1 = CSTR
  79.     ;-> d0 = 0 if equal
  80.     ;***
  81. CompareBC:
  82.         tst.b        (a2)
  83.         beq.s        NotEqualBC
  84.         moveq        #0,d0
  85. LoopBC:
  86.         cmp.b        (a2),d0
  87.         bgt.s        EqualBC
  88.         move.b    (a1,d0),d1
  89.         cmp.b        1(a2,d0),d1
  90.         bne.s        NotEqualBC
  91.         addq.w    #1,d0
  92.         bra.s        LoopBC
  93. EqualBC:
  94.         moveq        #0,d0
  95.         rts
  96. NotEqualBC:
  97.         moveq        #-1,d0
  98.         rts
  99.  
  100.     ;*** Search for a task ***
  101.     ;d0 = Process number
  102.     ;a0 = Task pointer
  103.     ;-> a0 = task pointer or NULL if not found
  104.     ;***
  105. Search:
  106.         cmp.l        #0,a0                    ;Not the end of the tasks ?
  107.         beq.s        TheEndS
  108.         cmp.b        #NT_PROCESS,LN_TYPE(a0)
  109.         bne.s        NextTaskS
  110.         cmp.l        pr_TaskNum(a0),d0
  111.         beq.s        TheEndS
  112. NextTaskS:
  113.         move.l    LN_SUCC(a0),a0
  114.         bra.s        Search
  115. TheEndS:
  116.         rts
  117.  
  118.     ;*** Search a task when the loaded commandname is given ***
  119.     ;a1 = Pointer to name
  120.     ;a0 = Task pointer
  121.     ;-> a0 = task pointer or NULL if not found
  122.     ;***
  123. SearchS:
  124.         cmp.l        #0,a0
  125.         beq.s        TheEndSS
  126.         cmp.b        #NT_PROCESS,LN_TYPE(a0)
  127.         bne.s        NextTaskSS
  128.         move.l    pr_CLI(a0),a2
  129.         add.l        a2,a2
  130.         add.l        a2,a2
  131.         cmp.l        #0,a2
  132.         beq.s        NextTaskSS
  133.         move.l    cli_CommandName(a2),a2
  134.         add.l        a2,a2
  135.         add.l        a2,a2
  136.         bsr        CompareBC
  137.         beq.s        TheEndSS
  138. NextTaskSS:
  139.         move.l    LN_SUCC(a0),a0
  140.         bra.s        SearchS
  141. TheEndSS:
  142.         rts
  143.  
  144.     ;*** Skip spaces ***
  145.     ;a0 = string
  146.     ;-> a0 = after last space
  147.     ;***
  148. SkipSpace:
  149.         tst.b        (a0)
  150.         beq.s        EndOfString
  151.         cmp.b        #' ',(a0)+
  152.         beq.s        SkipSpace
  153.         sub.l        #1,a0
  154. EndOfString:
  155.         rts
  156.  
  157.     ;*** Skip non spaces ***
  158.     ;a0 = string
  159.     ;-> a0 = points to first space
  160.     ;***
  161. SkipNSpace:
  162.         tst.b        (a0)
  163.         beq.s        EndOfString
  164.         cmp.b        #' ',(a0)+
  165.         bne.s        SkipNSpace
  166.         sub.l        #1,a0
  167.         rts
  168.  
  169.     ;*** Check if a byte is a digit ***
  170.     ;(a0) byte to check
  171.     ;-> d0 = 0 if digit
  172.     ;***
  173. IsDigit:
  174.         cmp.b        #'0',(a0)
  175.         blt.s        NoDigitID
  176.         cmp.b        #'9',(a0)
  177.         bgt.s        NoDigitID
  178.         moveq        #0,d0
  179.         rts
  180. NoDigitID:
  181.         moveq        #-1,d0
  182.         rts
  183.  
  184.     ;***==============***
  185.     ;*** Main program ***
  186.     ;***==============***
  187. Main:
  188.         move.l    ArgPointer,a0
  189.         move.b    #0,Number            ;Assume no process number is given
  190.         bsr        SkipSpace
  191.         tst.b        (a0)
  192.         beq        Error
  193.         bsr        IsDigit
  194.         bne.s        NoDigit
  195.     ;A digit so a process number must be given
  196.         move.b    #1,Number            ;Sorry, I was wrong. It was a process number
  197.         moveq        #0,d2
  198.         move.b    (a0),d2
  199.         sub.b        #'0',d2
  200.         lea        1(a0),a0
  201.         bsr        IsDigit
  202.         bne.s        NoDigitA
  203.         mulu        #10,d2
  204.         add.b        (a0),d2
  205.         sub.b        #'0',d2                ;We stop here, maximum 2 digits
  206.         lea        1(a0),a0
  207. NoDigitA:
  208.         move.l    d2,ProcNum
  209.         cmp.b        #' ',(a0)
  210.         bne        Error
  211.         bra.s        Contin
  212.     ;No digit so a loaded command name is given
  213. NoDigit:
  214.         move.l    a0,CommAdd
  215.         bsr        SkipNSpace
  216.         tst.b        (a0)
  217.         beq        Error
  218.         move.b    #0,(a0)                ;End the string here
  219.         lea        1(a0),a0
  220. Contin:
  221.         bsr        SkipSpace
  222.         tst.b        (a0)
  223.         beq        Error
  224.     ;Now we must scan the signal number
  225.         moveq        #0,d2
  226.         bsr        IsDigit
  227.         bne        Error
  228.         move.b    (a0),d2
  229.         sub.b        #'0',d2
  230.         lea        1(a0),a0
  231.         bsr        IsDigit
  232.         bne.s        Continue
  233.         mulu        #10,d2
  234.         add.b        (a0),d2
  235.         sub.b        #'0',d2
  236.         lea        1(a0),a0
  237.         bsr        IsDigit
  238.         bne.s        Continue
  239.         mulu        #10,d2
  240.         add.b        (a0),d2
  241.         sub.b        #'0',d2
  242. Continue:
  243.         move.l    d2,SigNum
  244.     ;Search the task
  245.         CALLEXEC    Forbid
  246.         tst.b        Number
  247.         bne.s        SearchNum
  248.         move.l    CommAdd,a1
  249.         move.l    (SysBase).w,a0
  250.         add.l        #TaskReady,a0
  251.         bsr        SearchS
  252.         cmp.l        #0,a0
  253.         bne.s        FoundIt
  254.         move.l    (SysBase).w,a0
  255.         add.l        #TaskWait,a0
  256.         bsr        SearchS
  257.         cmp.l        #0,a0
  258.         beq.s        NotFound
  259.         bra.s        FoundIt
  260. SearchNum:
  261.         move.l    ProcNum,d0
  262.         move.l    (SysBase).w,a0
  263.         add.l        #TaskReady,a0
  264.         bsr        Search
  265.         cmp.l        #0,a0
  266.         bne.s        FoundIt
  267.         move.l    (SysBase).w,a0
  268.         add.l        #TaskWait,a0
  269.         bsr        Search
  270.         cmp.l        #0,a0
  271.         beq.s        NotFound
  272. FoundIt:
  273.         move.l    a0,a2
  274.         CALLEXEC    Permit
  275.     ;Signal the task
  276.         move.l    a2,a1
  277.         move.l    SigNum,d0
  278.         CALLEXEC    Signal
  279.         rts
  280. NotFound:
  281.         CALLEXEC    Permit
  282.         move.l    OutputHandle,d1
  283.         move.l    #ProcessStr,d2
  284.         move.l    #ProcessStrLen,d3
  285.         CALLDOS    Write
  286.         rts
  287. Error:
  288.         move.l    OutputHandle,d1
  289.         move.l    #UsageStr,d2
  290.         move.l    #UsageStrLen,d3
  291.         CALLDOS    Write
  292.         rts
  293.  
  294.  
  295.     EVEN
  296.  
  297. DosBase:            dc.l    0
  298. OutputHandle:    dc.l    0
  299. ArgPointer:        dc.l    0                ;Argument pointer given by DOS
  300. ArgLen:            dc.l    0                ;Length of ArgPointer
  301. ProcNum:            dc.l    0                ;Process number to search
  302. CommAdd:            dc.l    0                ;Command string to search
  303. SigNum:            dc.l    0                ;Number to signal
  304.  
  305. Number:            dc.b    0                ;1 if a process number is given
  306.  
  307.     ;Library names
  308. DosLib:            dc.b    "dos.library",0
  309.  
  310. UsageStr:        dc.b    "Usage: Signal <Process ID>|<Program Name> <Signal Number>",10,0
  311. UsageStrLen        equ    *-UsageStr
  312.  
  313. ProcessStr:        dc.b    "Process does not exist !",10,0
  314. ProcessStrLen    equ    *-ProcessStr
  315.  
  316.     END
  317.  
  318.